const express = require("express");
const router = express.Router();
const handle = require("../routes_handle/space"); // 假设业务逻辑在这里
const { CODE, Response } = require("../utils/response");

// 1. 获取分组列表
router.get("/group/getGroupList", async (req, res, next) => {
  try {
    const result = await handle.getGroupList();
    res.status(CODE.SUCCESS).json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 2. 新增分组
router.post("/group/addGroup", async (req, res, next) => {
  try {
    const { FName, FIsdefault } = req.body;
    if (!FName) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少分组名称"));
    }
    const result = await handle.addGroup(FName, FIsdefault);
    res.status(CODE.SUCCESS).json(Response.success(result, "新增成功"));
  } catch (err) {
    next(err);
  }
});

// 3. 编辑分组
router.post("/group/updateGroup", async (req, res, next) => {
  try {
    const { FId, FName, FIsdefault } = req.body;
    if (!FId || !FName) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少分组ID或名称"));
    }
    const result = await handle.updateGroup({ FId, FName, FIsdefault });
    res.status(CODE.SUCCESS).json(Response.success(result, "编辑成功"));
  } catch (err) {
    next(err);
  }
});

// 4. 删除分组
router.post("/group/deleteGroup", async (req, res, next) => {
  try {
    const { FId } = req.body;
    if (!FId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少分组ID"));
    }
    const result = await handle.deleteGroup(FId);
    res.status(CODE.SUCCESS).json(Response.success(result, "删除成功"));
  } catch (err) {
    next(err);
  }
});

// 5. 获取空间列表（按分组）
router.get("/space/getSpaceListByGroup", async (req, res, next) => {
  try {
    const { FGroupId, FUserId } = req.query;
    const result = await handle.getSpaceListByGroup({ FGroupId, FUserId });
    res.status(CODE.SUCCESS).json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 6. 搜索空间
router.get("/space/searchSpace", async (req, res, next) => {
  try {
    const { FKeyword, FUserId } = req.query;
    if (!FKeyword) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少搜索关键词"));
    }
    const result = await handle.searchSpace({ FKeyword, FUserId });
    res.status(CODE.SUCCESS).json(Response.success(result, "搜索成功"));
  } catch (err) {
    next(err);
  }
});

// 7. 创建空间
router.post("/space/createSpace", async (req, res, next) => {
  try {
    const { FTitle, FDescription, FGroupId, FTag, FCreatorId, FCover } =
      req.body;
    if (!FTitle || !FDescription || !FGroupId || !FCreatorId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少必要参数"));
    }
    const result = await handle.createSpace({
      FTitle,
      FDescription,
      FGroupId,
      FTag,
      FCreatorId,
      FCover,
    });
    res.status(CODE.SUCCESS).json(Response.success(result, "创建成功"));
  } catch (err) {
    next(err);
  }
});

// 8. 切换收藏状态
router.post("/space/toggleFavorite", async (req, res, next) => {
  try {
    const { FSpaceId, FUserId, FIsStarred } = req.body;
    if (FSpaceId == null || FUserId == null || FIsStarred == null) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少必要参数"));
    }
    const result = await handle.toggleFavorite({
      FSpaceId,
      FUserId,
      FIsStarred,
    });
    res.status(CODE.SUCCESS).json(Response.success(result, "操作成功"));
  } catch (err) {
    next(err);
  }
});

// 9. 获取空间详情
router.get("/space/getSpaceDetail", async (req, res, next) => {
  try {
    const { FId } = req.query;
    if (!FId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID"));
    }
    const result = await handle.getSpaceDetail(FId);
    res.status(CODE.SUCCESS).json(Response.success(result, "获取成功"));
  } catch (err) {
    next(err);
  }
});

// 10. 更新空间信息
router.post("/space/updateSpace", async (req, res, next) => {
  try {
    const { FId, FTitle, FDescription, FGroupId, FTag, FCover, FModifierId } =
      req.body;
    if (!FId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID"));
    }

    // 至少需要一个更新字段
    if (!FTitle && !FDescription && !FGroupId && !FTag && !FCover) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "至少需要一个更新字段"));
    }

    const result = await handle.updateSpace({
      FId,
      FTitle,
      FDescription,
      FGroupId,
      FTag,
      FCover,
      FModifierId,
    });
    res.status(CODE.SUCCESS).json(Response.success(result, "更新成功"));
  } catch (err) {
    next(err);
  }
});

// 11. 删除空间
router.post("/space/deleteSpace", async (req, res, next) => {
  try {
    const { FId } = req.body;
    if (!FId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID"));
    }
    const result = await handle.deleteSpace(FId);
    res.status(CODE.SUCCESS).json(Response.success(result, "删除成功"));
  } catch (err) {
    next(err);
  }
});

// 12. 添加空间管理员
router.post("/space/addAdmin", async (req, res, next) => {
  try {
    const { FSpaceId, FUserId } = req.body;
    if (!FSpaceId || !FUserId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID或用户ID"));
    }

    const result = await handle.addSpaceAdmin(FSpaceId, FUserId);
    res.status(CODE.SUCCESS).json(Response.success(result, "添加管理员成功"));
  } catch (err) {
    next(err);
  }
});

// 13. 移除空间管理员
router.post("/space/removeAdmin", async (req, res, next) => {
  try {
    const { FSpaceId, FUserId } = req.body;
    if (!FSpaceId || !FUserId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID或用户ID"));
    }

    const result = await handle.removeSpaceAdmin(FSpaceId, FUserId);
    res.status(CODE.SUCCESS).json(Response.success(result, "移除管理员成功"));
  } catch (err) {
    next(err);
  }
});

// 14. 获取空间管理员列表
router.get("/space/getAdmins", async (req, res, next) => {
  try {
    const { FSpaceId } = req.query;
    if (!FSpaceId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID"));
    }

    const result = await handle.getSpaceAdmins(FSpaceId);
    res
      .status(CODE.SUCCESS)
      .json(Response.success(result, "获取管理员列表成功"));
  } catch (err) {
    next(err);
  }
});

// 15. 检查用户是否是空间管理员
router.get("/space/checkAdmin", async (req, res, next) => {
  try {
    const { FSpaceId } = req.query;
    const FUserId  = req.user.userId
    if (!FSpaceId || !FUserId) {
      return res
        .status(CODE.BAD_REQUEST)
        .json(Response.error(CODE.BAD_REQUEST, "缺少空间ID"));
    }

    const isAdmin = await handle.isSpaceAdmin(FUserId, FSpaceId);
    res.status(CODE.SUCCESS).json(Response.success({ isAdmin }, "检查成功"));
  } catch (err) {
    next(err);
  }
});

module.exports = router;
